﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace AREQWinService
{
    class clsSharePointHotels
    {
        public class Hotel
        {
            public string IATACity { get; set; }
            public string HotelName { get; set; }
            public string Street { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zip { get; set; }
            public string Phone { get; set; }
            public string GroundTransport { get; set; }
            public string PickupLocation { get; set; }
            public string TimeSpecial { get; set; }
            public string Discount { get; set; }

        }
        private static void BypassCertificateError()
        {
            System.Net.ServicePointManager.ServerCertificateValidationCallback +=

                delegate(
                    Object sender1,
                    X509Certificate certificate,
                    X509Chain chain,
                    SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };
        }
        public List<Hotel> getAllHotelData()
        {
            SPWebReference.Lists myservice = new SPWebReference.Lists();
            myservice.Credentials = new System.Net.NetworkCredential("emlpilot", "emlpilot", "jetblue");
           // myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
            //myservice.Credentials = new System.Net.NetworkCredential("ccook", "ChuckandKaty@", "jetblue");
            myservice.Url = "https://sites.jetblue.com/sites/Flight%20Operations/InflightSvcLeadership/NewHotelCommittee/_vti_bin/Lists.asmx";
            try
            {
               // using (new Impersonator("APP_AREQ2", "jetblue", "HappyJetting!10"))
               // {
                    /* Assign values to pass the GetListItems method
                    So how do you turn %7B6E5EDCC1%2DAD6A%2D4767%2DBF1D%2DB49EFCA90103%7D into {6e5edcc1-ad6a-4767-bf1d-b49efca90103}.
                    You need to replace “%7B” with “{“, “%2D” with “-” and “%7D” with “}”
                     */
                    string listName = "{5A5CAB4C-C064-457A-A3E4-E4BDD4A7F200}"; //NewHotelCommittee/Lists/hotels_cmtk
                    string viewName = ""; //"{D3B5A5BD-C71E-44C0-904D-174097DC5FCA}"; //NewHotelCommittee/Lists/hotels_cmtk

                    string rowLimit = "1000";

                    // Instantiate an XmlDocument object
                    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                    System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
                    System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
                    System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");

                    /*Use CAML query*/
                    //query.InnerXml = "<Where><Gt><FieldRef Name=\"Title\" />" +
                    //"<Value Type=\"string\">" + IATACity + "</Value></Gt></Where>";
                    //viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
                    queryOptions.InnerXml = "";

                    BypassCertificateError();
                    System.Xml.XmlNode nodes = myservice.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, null);
                    List<Hotel> hotels = new List<Hotel>();

                    foreach (System.Xml.XmlNode node in nodes)
                    {
                        if (node.Name == "rs:data")
                        {
                            for (int i = 0; i < node.ChildNodes.Count; i++)
                            {
                                if (node.ChildNodes[i].Name == "z:row")
                                {
                                    Hotel newHotel = new Hotel();
                                    foreach (XmlAttribute attr in node.ChildNodes[i].Attributes)
                                    {

                                        /* 
                                         * Get attribute names from List Settings .. Click on Column Name ..
                                         * Look in URL at the end for &Field=
                                         * Add ows_ in front of Field name. Case sensitive
                                         * Example url://..2A%7D&Field=Title
                                         * equals ows_Title
                                         */
                                        if (attr.Name == "ows_Title") //Default first field is always called Title (case sensitive)
                                            newHotel.IATACity = attr.Value;
                                        if (attr.Name == "ows_HOTEL")
                                            newHotel.HotelName = attr.Value;
                                        if (attr.Name == "ows_STREET")
                                            newHotel.Street = attr.Value;
                                        if (attr.Name == "ows_CITY")
                                            newHotel.City = attr.Value;
                                        if (attr.Name == "ows_STATE")
                                            newHotel.State = attr.Value;
                                        if (attr.Name == "ows_ZIP")
                                            newHotel.Zip = attr.Value;
                                        if (attr.Name == "ows_PHONE")
                                            newHotel.Phone = attr.Value;
                                        if (attr.Name == "ows_FAX")
                                            newHotel.GroundTransport = attr.Value;
                                        if (attr.Name == "ows_MISC")
                                            newHotel.PickupLocation = attr.Value;
                                        if (attr.Name == "ows_TIMESPECIAL")
                                            newHotel.TimeSpecial = attr.Value;
                                        if (attr.Name == "ows_STAY")
                                            newHotel.Discount = attr.Value;
                                    }
                                    hotels.Add(newHotel);
                                }
                            }
                        }
                    }
                    return hotels;
                }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return null;
            }
        }
    }
}
